﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Web;
using TrafficArchives.Common;

namespace TrafficArchives.DataAccess
{
    /// <summary>
    /// 安全教育数据库操作。
    /// </summary>
    public class SafeEducateDb : DbBase
    {
        public override PaginationData<DataTable> GetData(Request request)
        {
            var paginationData = new PaginationData<DataTable>();
            var normalSql = "select t.*, rownum as rowno from AQJYTABLE t where " + request.ToSql();
            var sql = request.Pagination.Enabled
                          ? string.Format("select * from ({0}) A where A.rowno > {1} and A.rowno <= {2}", normalSql,
                                          GetPageStart(request.Pagination), GetPageEnd(request.Pagination))
                          : normalSql;
            paginationData.Data = ExecuteDataTable(sql);
            if (request.Pagination.Enabled)
            {
                paginationData.RecordCount = ExecuteCount(normalSql.Replace("t.*, rownum as rowno", "count(*)"));
            }
            return paginationData;
        }

        /// <summary>
        /// 根据Id取安全教育数据。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public override DataTable GetData(string id)
        {
            var request = new Request();
            request.Items.Add(new Condition
            {
                FieldName = "AQJYTABLE_ID",
                Operator = ConditionOperator.Equal,
                Values = new object[] { id }
            });
            return GetData(request).Data;
        }

        /// <summary>
        /// 取表结构。
        /// </summary>
        /// <returns></returns>
        public override DataTable GetNoneData()
        {
            var request = new Request();
            request.Items.Add(new Condition
            {
                FieldName = "AQJYTABLE_ID",
                Operator = ConditionOperator.Equal,
                Values = new object[] { "null" }
            });
            return GetData(request).Data;
        }

        public override void Add(DataTable data)
        {
            if (data.Rows.Count == 0)
            {
                return;
            }
            const string sql = "insert into AQJYTABLE(AQJYTABLE_ID,NAME,PXDATE,PXADDRESS,JYPERSON,PXYYID,PXYYNAME,PXCONTENT)" +
                               "values(:AQJYTABLE_ID,:NAME,:PXDATE,:PXADDRESS,:JYPERSON,:PXYYID,:PXYYNAME,:PXCONTENT)";
            var parameters = new[]{
                                    MakeParam(":AQJYTABLE_ID", OracleType.VarChar, 40, "AQJYTABLE_ID"),
                                     MakeParam(":NAME", OracleType.VarChar, 20, "NAME"),
                                     MakeParam(":PXDATE", OracleType.DateTime, 8, "PXDATE"),
                                     MakeParam(":PXADDRESS", OracleType.VarChar, 100, "PXADDRESS"),
                                     MakeParam(":JYPERSON", OracleType.VarChar, 50, "JYPERSON"),
                                     MakeParam(":PXYYID", OracleType.VarChar, 40, "PXYYID"),
                                     MakeParam(":PXYYNAME", OracleType.VarChar, 40, "PXYYNAME"),
                                     MakeParam(":PXCONTENT", OracleType.VarChar, 200, "PXCONTENT")
                                  };
            OnAdd(data, sql, parameters);
        }

        public override void Update(DataTable data)
        {
            if (data.Rows.Count == 0)
            {
                return;
            }
            const string sql = "update AQJYTABLE set " +
                               "NAME = :NAME," +
                               "PXDATE = :PXDATE," +
                               "PXADDRESS = :PXADDRESS," +
                               "JYPERSON = :JYPERSON," +
                               "PXYYID = :PXYYID," +
                               "PXYYNAME = :PXYYNAME," +
                               "PXCONTENT = :PXCONTENT" +
                               " where AQJYTABLE_ID = :AQJYTABLE_ID";
            var parameters = new[]{
                                     MakeParam(":NAME", OracleType.VarChar, 20, "NAME"),
                                     MakeParam(":PXDATE", OracleType.DateTime, 8, "PXDATE"),
                                     MakeParam(":PXADDRESS", OracleType.VarChar, 100, "PXADDRESS"),
                                     MakeParam(":JYPERSON", OracleType.VarChar, 50, "JYPERSON"),
                                     MakeParam(":PXYYID", OracleType.VarChar, 40, "PXYYID"),
                                     MakeParam(":PXYYNAME", OracleType.VarChar, 40, "PXYYNAME"),
                                     MakeParam(":PXCONTENT", OracleType.VarChar, 200, "PXCONTENT"),
                                     MakeParam(":AQJYTABLE_ID", OracleType.VarChar, 40, "AQJYTABLE_ID")
                                  };
            OnUpdate(data, sql, parameters);
        }

        public override void Delete(List<string> ids)
        {
            const string sql = "delete from AQJYTABLE t where t.AQJYTABLE_ID in ('{0}')";
            ExecuteNoneQuery(string.Format(sql, string.Join("','", ids)));
        }
    }
}